package com.sky.controller.employee;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/employee")
@Api(tags = "管理端相关接口")
@Slf4j
public class EmployeeController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private EmployeeService employeeService;
    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "管理端员工登录")
    public Result<EmployeeVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        System.out.println("管理端登录数据------>>"+employeeLoginDTO.getEmployeeName());
        log.info("管理端员工登录:{}",employeeLoginDTO);
        //根据获得到的用户数据进行登录
        Employee employee=employeeService.login(employeeLoginDTO);
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        //生成token并获取
        String token= JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        //为返回给前端的数据赋值
        EmployeeVO employeeVO=EmployeeVO.builder()
                .id(employee.getId())
                .employeeName(employee.getEmployeeName())
                .image(employee.getImage())
                .token(token)
                .build();
        return Result.success(employeeVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }
}
